package com.maxima.maximaAttendance.actions;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.maxima.maximaAttendance.daos.TimeLogDAO;
import com.maxima.maximaAttendance.daos.UserDAO;
import com.maxima.maximaAttendance.pojos.TimeLog;
import com.maxima.maximaAttendance.pojos.User;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport implements SessionAware{

	private static final long serialVersionUID = 1L;
	private Integer id;
	private User user;
	private String password;
	private Map<String,Object> map;
	
	@Override
	public String execute() throws Exception {
		this.user=new UserDAO().getUser(this.getId());
		map.put("profile", user);
		map.put("usersList", new UserList().initUserList());
		this.setSession(map);
		return SUCCESS;
	}

	public String login() {
		
		this.password= ServletActionContext.getRequest().getParameter("password");
		this.user=(User)map.get("profile");
		TimeLogDAO timeLogDAO= new TimeLogDAO();
		if (this.user.getPassword().equals(password)) {
		
			Date date= new Date();
			
			SimpleDateFormat dateFormat= new SimpleDateFormat("dd-MM-yyyy");
			SimpleDateFormat timeFormat= new SimpleDateFormat("HH:mm:ss");

//			if(this.user.getLogged_in()==true)
//			{
//				TimeLog t=timeLogDAO.getLastTimeLog(this.user);
//				timeLogDAO.addTimeOut(t.getId(),date);
//				new UserDAO().setStatus(this.user.getId(), false);
//				this.map.put("message", "Logged out at " + timeFormat.format(date));
//				this.map.put("errorMessage", null);
//			}
//			else if(timeLogDAO.checkIfPresent(this.user,date)>0)
//			{
//				TimeLog t=timeLogDAO.getLastTimeLog(this.user);
//				timeLogDAO.addTimeOut(t.getId(),date);
//				new UserDAO().setStatus(this.user.getId(), false);
//				this.map.put("message", "Logged out at " + timeFormat.format(date));
//				this.map.put("errorMessage", null);
//			}
//			else
//			{
//				TimeLog t = new TimeLog(new Date(), this.user);
//				t.setLoginTime(new Date());
//				t.setComment("none");
//				timeLogDAO.addNewTimeLog(t);
//				new UserDAO().setStatus(this.user.getId(), true);
//				this.map.put("message", "Logged in at " + timeFormat.format(date));
//				this.map.put("errorMessage", null);
//			}
			map.put("usersList", new UserList().initUserList());
			this.setSession(map);
			return SUCCESS;
		}
		else
		{
			this.map.put("errorMessage", "Please enter correct password!!");
			this.map.put("message", null);
			this.setSession(map);
			return ERROR;
		}
	}
	public String changeUserPassword() {
		this.password= ServletActionContext.getRequest().getParameter("old_password");
		
		this.user=(User)map.get("profile");
		if (this.user.getPassword().equals(password)) {
			new UserDAO().changePassword(this.user.getId(),ServletActionContext.getRequest().getParameter("new_password") );
			this.map.put("message", "Your password has been changed successfully!!");
			this.map.put("errorMessage", null);
			return SUCCESS;
		}
		else
		{
			this.map.put("errorMessage", "Please enter correct password!!");
			this.map.put("message", null);
			this.setSession(map);
			return ERROR;
		}
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	@Override
	public void setSession(Map<String, Object> map) {
		this.map=map;
	}
	
	

}
